//
//  UITableView+NoData.m
//  EduOnLine
//
//  Created by Mac on 2019/11/14.
//  Copyright © 2019 eduonline. All rights reserved.
//

#import "UITableView+NoData.h"

NSString const *UIButton_actionKey = @"UIButton_actionKey";

@implementation UITableView (NoData)

- (UIView *)noDataView:(NoDataType)type typeDes:(NSString *)des block:(BtnActionBlock)block{
    
        UIView *noDataView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, kUI_WINDOW_H)];
        [noDataView setBackgroundColor:[UIColor clearColor]];
        
        UIImageView *iconV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:type == NoData?@"wu":@"nosignal"]];
        [noDataView addSubview:iconV];
    
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectZero];
        [lab setText:des?des:(type == NoData? @"哎呀，什么都没有～":@"哎呀，网络链接出现了问题")];
        [lab setTextColor:[UIColor colorWithHexString:@"#666666"]];
        [lab setTextAlignment:NSTextAlignmentCenter];
        [lab setFont:[UIFont systemFontOfSize:15]];
        [noDataView addSubview:lab];
    
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn.layer setMasksToBounds:YES];
        [btn.layer setCornerRadius:17];
        [btn.layer setBorderWidth:1];
        [btn.layer setBorderColor:[UIColor colorWithHexString:@"#FF6E5D"].CGColor];
        [btn setTitle:@"重新加载" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
        [noDataView addSubview:btn];
        
        [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(130,85));
            make.centerX.mas_offset(0);
            make.centerY.mas_offset(-85/2);
            //make.top.mas_offset(100);
        }];
        
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.top.equalTo(iconV.mas_bottom).offset(15);
            make.height.mas_equalTo(20);
        }];
    
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(125, 34));
            make.centerX.mas_offset(0);
            make.top.equalTo(lab.mas_bottom).offset(15);
        }];

        if (block) {
            [btn setHidden:NO];
        }else{
            [btn setHidden:YES];
        }
    
        return noDataView;
}

- (void)setNoDataType:(NoDataType)type typeDes:(NSString *)des{
    if (type == hasData) {
        [self.backgroundView setHidden:YES];
        self.scrollEnabled = YES;
    }else{
        self.backgroundView = [self noDataView:type typeDes:des block:nil];
        [self.backgroundView setHidden:NO];
        self.scrollEnabled = YES;
    }
}

- (void)setNoDataType:(NoDataType)type typeDes:(NSString *)des block:(BtnActionBlock)block{
    if (type == hasData) {
        [self.backgroundView setHidden:YES];
        self.scrollEnabled = YES;
    }else{
        self.actionBlock = block;
        self.backgroundView = [self noDataView:type typeDes:des block:block];
        [self.backgroundView setHidden:NO];
        self.scrollEnabled = YES;
    }
}

- (void)btnAction{
    if (self.actionBlock) {
        self.actionBlock();
    }
}

- (BtnActionBlock)actionBlock{
     return objc_getAssociatedObject(self, &UIButton_actionKey);
}

- (void)setActionBlock:(BtnActionBlock)actionBlock{
    objc_setAssociatedObject(self, &UIButton_actionKey, actionBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
